<template>
	<view :class="minishop.config.style">
		<FullscreenLoading :loading="loading" :config="minishop.config"></FullscreenLoading>
		<!-- #ifdef H5-->
		<Top :config="minishop.config" />
		<!-- #endif -->
		<view class="main-section tabbar-bottom top">
			<view class="cover">
				<swiper class="swiper" @change="swiperChange"  :autoplay="false">
					<swiper-item v-if="data.video">
						<view class="video-cover" :class="videoPlaying?'hidden':''" @click="playVideo">
							<image mode="widthFix" :src="data.cover_800" class="image"></image>
							<view class="play-btn">
								<text class="iconfont play">&#xe623;</text>
							</view>
						</view>
						<view class="video-player" :class="!videoPlaying?'hidden':''">
							<video id="coverVideo" class="video" :src="data.video_str" @pause="pauseVideo"/>
						</view>
					</swiper-item>
					<swiper-item v-else>
						<view class="video-cover">
							<image mode="widthFix" :src="data.cover_800" class="image"></image>
						</view>
					</swiper-item>
					<swiper-item v-for="(item,index) in data.images_format" :key="index">
						<view class="swiper-item" @click="util.goToLink(item.link)">
							<image mode="widthFix" :src="item.format" class="image"></image>
						</view>
					</swiper-item>
				</swiper>
				<view class="current">
					<text>{{current}}</text>/<text>{{coverTotal}}</text>
				</view>
			</view>

			<view class="base-info">
				<view class="data-info">
					<view class="price">
						<text>￥</text>
						<text class="pointback">{{handlePrice(data.price,0)}}</text>
						<text>.</text>
						{{handlePrice(data.price,1)}}
					</view>
					<view class="marking-price">
						￥ {{data.marking_price}}
					</view>
				</view>
				<view class="exp-info">
					<view class="item view">
						<uni-icons type="eye-filled" size="10" color="#d8d8d8"></uni-icons> 
						<text class="num">{{data.handling_view}}</text>
					</view>
					<view class="item sales">
						<uni-icons type="cart-filled" size="10" color="#d8d8d8"></uni-icons> 
						<text class="num">{{data.handling_sales}}</text>
					</view>
					<view class="item favorites">
						<uni-icons type="star-filled" size="10" color="#d8d8d8"></uni-icons> 
						<text class="num">{{data.handling_favorites}}</text>
					</view>
				</view>
				<view class="title">
					{{data.title}}
				</view>
				<view class="description">
					{{data.description}}
				</view>
			</view>

			<!--支持的会员卡-->
			<template v-if="!loading && data.support_member_cards!==undefined && data.support_member_cards.length > 0">
				<SupportMemberCards :cardsData="data.support_member_cards" />
			</template>
			<!--评价功能-->
			<template  v-if="!loading">
				<Evaluate class="Evaluate" type="goods" :typeId="Number(data.id)" :config="minishop.config" />
			</template>
			<!--详情块-->
			<view class="detail">
				<view class="tab">
					<view class="tab-item tab-content">
						商品详情
					</view>
				</view>
				<view class="content">
					<!--完整描述-->
					<view class="all-content">
						<!-- #ifdef MP-->
						<rich-text :nodes="util.formatRichText(data.content)"></rich-text>
						<!-- #endif -->
						<!-- #ifdef H5-->
						<view v-html="util.formatRichText(data.content)"></view>
						<!-- #endif -->
					</view>
					<!--显示版权-->
					<CopyRight :data="minishop.config.config.copyright" />
				</view>
			</view>
		</view>
		<template v-if="!loading">
			<SaleBar ref="saleBar" type="MinishopGoods" :data="data" :config="minishop.config" />
		</template>
	</view>
</template>

<script>
	import Base from '@/muu_minishop/common/base.vue'

	import SaleBar from '../common/saleBar.vue'
	import SupportMemberCards from '../common/supportMemberCards.vue'
	import Evaluate from '../common/evaluate.vue'
	import CopyRight from '@/common/components/copyright.vue' //自定义版权

	export default {
		extends:Base,
		components: {
			SaleBar,
			SupportMemberCards,
			Evaluate,
			CopyRight,
		},
		data() {
			return {
				userInfo : {}, //用户数据
				data: {}, //商品数据
				//轮播图相关数据
				videoPlaying: false, //视频播放状态
				current: 1, //轮播第N个索引
				coverTotal: 1,
			}
		},
		async onLoad(option) { //option为object类型，会序列化上个页面传递的参数
			await this.$onLaunched
			await this.baseIsReady()

			//#ifdef MP-WEIXIN
			//通过小程序码进入的获取参数
			if(option.scene){
				// options 中的 scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
				let scene = decodeURIComponent(option.scene); 
				let obj = {};
				for (var i = 0; i < scene.split('*').length;i++){
				  var arr = scene.split('*')[i].split('/');
				  obj[arr[0]] = arr[1];
				}
			
				this.urlParams.id = obj.id
				this.urlParams.promoter_uid = obj.u
			}
			//#endif
			
			Promise.all([this.getData()]).then(res => {
				// H5端执行
				//#ifdef H5
				let url = window.location.href
				let shareData = {
					title : this.data.title,
					desc : this.data.description,
					img: this.data.cover_200
				}
				this.wechat.share(shareData,url);
				//#endif
				
				this.loading = false
			})
			
			//实例化视频播放器
			this.videoContext = uni.createVideoContext('coverVideo')
		},
		created() {
			//获取已登录用户数据
			this.getUserInfo()
		},

		methods: {
			
			/**
			 * 播放轮播内视频
			 */
			playVideo(){
				this.videoContext.play()
				this.videoPlaying = true
			},
			/**
			 * 播放轮播内视频暂停
			 */
			pauseVideo(){
				this.videoContext.pause()
				this.videoPlaying = false
			},
			
			/**
			 * 获取商品详情数据
			 */
			getData(){
				let that = this
				let params = this.urlParams
				return new Promise((resolve, reject) => {
					let data = {}
						data.action = 'detail'
						data.id = params.id
						if(params.hasOwnProperty("promoter_uid") && params.promoter_uid !== undefined){
							data.promoter_uid = params.promoter_uid
						}
					//获取首页数据
					let url = '';
						//#ifdef MP-WEIXIN
						url = 'entry/wxapp/goods'
						//#endif
						//#ifdef H5
						url = 'entry/site/goods'
						//#endif
					this.util.request({
						url : url,
						data : data,
						success: (res) => {
							if (res.data.code == 200) {
								that.data = res.data.data
								uni.setNavigationBarTitle({
								　　title:res.data.data.title
								})
								if(res.data.data.images && res.data.data.images instanceof Array){
									that.coverTotal = res.data.data.images.length + 1
								}
								resolve(res.data)
							}
						}
					})
				})
			},
			
			/**
			 * 获取已登录用户数据
			 */
			getUserInfo(){
				let that = this
				let userInfo = uni.getStorageSync('userInfo') || {}
				this.userInfo = userInfo
			},
			
			swiperChange(e){
				console.log(e.detail)
				this.current = Number(e.detail.current) + 1
			},
			swiperCurrentIndex(e){
				//console.log(e.detail)
			},
			handlePrice(price,index){
				return (price || "").split('.')[index];
			},
			isEmptyObject(obj) {
				for(var key in obj){
					break;  return false
				};
				return true  
			},
		},
		
		onShow() {
			//this.$refs.saleBar.closeSaleChoose()
		},
		onReachBottom() {
			console.log('页面滚动到底部')
		},
		
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage() {
			// 分享(分销)用户ID
			let promoterUid = 0
			if(!this.isEmptyObject(this.userInfo) && this.userInfo.memberInfo.uid){
				promoterUid = this.userInfo.memberInfo.uid
			}
			let share = {
				title : this.data.title,
				path : '/muu_minishop/pages/goods/detail?' + 'id=' + this.data.id + '&promoter_uid=' + promoterUid,
				desc : this.data.description,
				success : function(){
					console.log(promoterUid)
				},
			}
			
			return share
		},
		/**
		 * 监听用户点击右上角转发到朋友圈
		 */
		onShareTimeline() {
			
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #f5f6f7;
	}
	.cover {
		position: relative;
		width: 750rpx;
		height: 750rpx;
		image {
			width: 100%;
			display: block;
		}
		.swiper {
			width: 750rpx;
			height: 750rpx;
			.swiper-item {
				image {
					width: 750rpx;
					height: 750rpx;
				}
			}
			.video-cover {
				position: relative;
				.play-btn {
					position: absolute;
					left: 50%;
					top: 50%;
					margin-left: -50rpx;
					margin-top: -50rpx;
					width: 100rpx;
					height: 100rpx;
					
					.play {
						font-size: 100rpx;
						color: #fff;
					}
				}
			}
			.video-player {
				background-color: #000;
				padding: 150rpx 0;
				.video {
					width: 750rpx;
					height: 450rpx;
				}
			}
		}
		.current {
			position: absolute;
			right: 10rpx;
			bottom: 10rpx;
			width: 100rpx;
			height: 48rpx;
			border-radius: 20rpx;
			background: rgba(0,0,0,0.2);
			text-align: center;
			line-height: 48rpx;
			font-size: 24rpx;
			color: #fff;
		}
	}
	.base-info {
		background-color: #fff;
		padding: 20rpx;
		margin-bottom: 24rpx;
		border-radius:  0 0 14rpx 14rpx;
		.data-info {
			// display: flex;
			display: inline-block;
			width: 50%;
			.price {
				font-size: 30rpx;
				font-weight: 600;
				margin: 20rpx 0 10rpx 0;
				display: inline-block;
				font-family: 'DIN-Medium, DIN';
				.pointback{
					font-size: 60rpx;
				}
			}
			.marking-price {
				font-size: 28rpx;
				color: #999;
				text-decoration: line-through;
				display: inline-block;
				padding-left: 20rpx;
			}
		}
		.title {
			font-size: 30rpx;
			font-weight: 600;
			color: #2d2e2f;
			font-weight: 600;
			font-family: 'PingFangSC-Medium, PingFang SC';
		}
		.description {
			font-size: 24rpx;
			color: #8c8d8e;
			margin-top: 10rpx;
		}
		.exp-info {
			flex: 1;
			width: 50%;
			text-align: right;
			display: inline-block;
			.item {
				display: inline-block;
				color: #999;
				margin: 0 10rpx;
				font-size: 24rpx;
				.num {
					margin-left: 10rpx;
				}
			}
		}
	}
	// .coupon{
	// 	.reducedPrice{
	// 		display: inline-block;
	// 		width: 100%;
	// 		height: 161rpx;
	// 		background-color: #fff;
	// 		border-radius: 14rpx;
			
	// 		.reducedText{
	// 			font-size: 24rpx;
	// 			font-weight: 600;
	// 			color: #2d2e2f;
	// 			line-height: 36rpx;
	// 			margin: 34rpx 0 0 33rpx;
	// 			display: inline-block;
	// 			margin-right: 40rpx;
	// 		}
	// 		.integrate{
	// 			display: inline-block;
	// 			font-size: 22rpx;
	// 			font-weight: 400rpx;
	// 			color: #2d2e2f;
	// 			line-height: 36rpx;
	// 		}
	// 	}
	// 	.elected{
	// 		width: 100%;
	// 		height: 84rpx;
	// 		background-color: #fff;
	// 		border-radius: 14rpx;
	// 		margin: 25rpx 0;
	// 		.selected{
	// 			font-size: 24rpx;
	// 			font-weight: 600;
	// 			color: #2d2e2f;
	// 			line-height: 36rpx;
	// 			margin: 22rpx 0 0 33rpx;
	// 			display: inline-block;
				
	// 		}
	// 		.specification{
	// 			display: inline-block;
	// 			font-size: 22rpx;
	// 			font-weight: 400rpx;
	// 			color: #2d2e2f;
	// 			line-height: 36rpx;
	// 			margin: 0 40rpx;
	// 			width: 70%;
				
	// 		}
	// 		.action{
	// 			display: inline-block;
	// 		}
	// 	}
	// }
	
		
	.Evaluate{
		border-radius: 14rpx;
	}
	.detail {
		.tab {
			display: flex;
			padding: 10rpx 0;
			padding: 0 20rpx 0 20rpx;
			background-color: #f5f6f7;
			.tab-item {
				flex: 1;
				text-align: center;
				line-height: 72rpx;
				padding-bottom: 8rpx;
				color: #2d2e2f;
				font-size: 28rpx;
				font-weight: 600;
			}
			.tab-item.only {
				flex: none;
				text-align: left;
			}
			.tab-content {
				
			}
			.tab-list {
				
			}
			.active {
				position: relative;
				&:before {
					position: absolute;
					right: 30%;
					left: 30%;
					bottom: 0;
					height: 8rpx;
					content: '';
					-webkit-transform: scaleY(.5);
					transform: scaleY(.5);
					background-color: #03b8cf;
				}
			}
		}
		//详情
		.content {
			background-color: #fff;
			.custom-desc {
				margin-bottom: 16rpx;
				padding: 20rpx;
				background-color: #fff;
			}
			.all-content {
				display: block;
				// padding: 20rpx;
				background-color: #fff;
				// border-radius: 60rpx;
				// .Large{
				// 	border-radius: 20rpx;
				// }
			}
			.copyright {
				background: #f8f8f8;
				padding: 20rpx;
				font-size: 24rpx;
				text-align: center;
				color: #666;
			}
		}
	}
	
	/*页面风格设计*/
	.Blue {
		.price {
			color: $Blue;
		}
	}
	.Green {
		.price {
			color: $Green;
		}
	}
	.LightRed {
		.price {
			color: $LightRed;
		}
	}
	.Orange {
		.price {
			color: $Orange;
		}
	}
	.LightPink {
		.price {
			color: $LightPink;
		}
	}
	.Magenta {
		.price {
			color: $Magenta;
		}
	}

</style>
